package com.example.sprintboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.sprintboot.entity.Product;
import com.example.sprintboot.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/admin/products")
@PreAuthorize("hasRole('ADMIN')")
public class AdminProductController {

    @Autowired
    private ProductService productService;

    // 商品列表
    @GetMapping
    public Map<String, Object> list(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) Integer categoryId
    ) {
        IPage<Product> result = productService.adminListProducts(page, pageSize, keyword, status, categoryId);
        Map<String, Object> response = new HashMap<>();
        response.put("list", result.getRecords());
        response.put("total", result.getTotal());
        response.put("page", result.getCurrent());
        response.put("pageSize", result.getSize());
        return response;
    }

    // 审核通过
    @PostMapping("/{id}/approve")
    public Map<String, Object> approve(@PathVariable Long id) {
        try {
            productService.approveProduct(id);
            return success("审核通过成功");
        } catch (Exception e) {
            return error("审核失败: " + e.getMessage());
        }
    }

    // 审核拒绝
    @PostMapping("/{id}/reject")
    public Map<String, Object> reject(@PathVariable Long id) {
        try {
            productService.rejectProduct(id);
            return success("审核拒绝成功");
        } catch (Exception e) {
            return error("审核失败: " + e.getMessage());
        }
    }

    // 上架
    @PostMapping("/{id}/on-sale")
    public Map<String, Object> onSale(@PathVariable Long id) {
        try {
            productService.onSaleProduct(id);
            return success("上架成功");
        } catch (Exception e) {
            return error("上架失败: " + e.getMessage());
        }
    }

    // 下架
    @PostMapping("/{id}/off-sale")
    public Map<String, Object> offSale(@PathVariable Long id) {
        try {
            productService.offSaleProduct(id);
            return success("下架成功");
        } catch (Exception e) {
            return error("下架失败: " + e.getMessage());
        }
    }

    // 编辑商品
    @PutMapping("/{id}")
    public Map<String, Object> update(@PathVariable Long id, @RequestBody Product product) {
        try {
            productService.updateProductByAdmin(id, product);
            return success("编辑成功");
        } catch (Exception e) {
            return error("编辑失败: " + e.getMessage());
        }
    }

    // 删除商品
    @DeleteMapping("/{id}")
    public Map<String, Object> delete(@PathVariable Long id) {
        try {
            productService.deleteProductByAdmin(id);
            return success("删除成功");
        } catch (Exception e) {
            return error("删除失败: " + e.getMessage());
        }
    }

    // 设置/取消首页轮播图
    @PostMapping("/{id}/banner")
    public Map<String, Object> setBanner(@PathVariable Long id, @RequestParam boolean isBanner) {
        boolean ok = productService.setBannerStatus(id, isBanner);
        if (ok) {
            return success(isBanner ? "已设为首页轮播图" : "已取消首页轮播图");
        } else {
            return error(isBanner ? "最多只能设置3个首页轮播图商品" : "操作失败");
        }
    }

    private Map<String, Object> success(String message) {
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", message);
        return response;
    }

    private Map<String, Object> error(String message) {
        Map<String, Object> response = new HashMap<>();
        response.put("success", false);
        response.put("message", message);
        return response;
    }
} 